//index.js
//获取应用实例
var app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    count: 0,
    textarea: "  这里放你个人的简介！",
    http: "https://www.baidu.com/",
    phone: "0760-xxxxxxx",
    position: '',
    company: '',
    inputValue: [{
        icon: "/images/shouji.png ",
        title: "手机：",
        value: "13539385314",
        p: "请输入",
        name: "tel"
      },
      {
        icon: "/images/weixin.png ",
        title: "微信：",
        value: "丁卡",
        p: "请输入",
        name: "weixin"
      },
      {
        icon: "/images/youxiang.png ",
        title: "邮箱：",
        value: "18423886287@qq.com",
        p: "请输入",
        name: "email"
      },
      {
        icon: "/images/dizhi.png ",
        title: "地址：",
        value: "fffff",
        p: "请点击右方按钮",
        name: "address",
        addr_icon: "/images/dizhi-tianchong.png "
      },
    ],
    index: "",
    imgs2: [],
    topPhoto: ''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  // 上传图片函数
  chooseImage: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          imgs: res.tempFilePaths
        })

        wx.uploadFile({
          url: app.ReqUrl + 'user/addPhotos',
          filePath: that.data.imgs[0],
          name: 'file',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Cookie': 'PHPSESSID=' + app.Cookie + ';path=/'
          },
          formData: {
            'openid_key': app.openid_key
          },
          success: res =>{

            // 拼接图片地址,获取图片addr
            that.setData({
              topPhoto: app.globalData.imgIP + res.data
            })
            console.log('图片地址：' + that.data.topPhoto);
            console.log(res);
          }
        })
      }
    })
  },

  // 上传多张图片
  chooseImage2: function() {
    var that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        that.setData({
          imgs2: that.data.imgs2.concat(res.tempFilePaths) //在原有基础上进行拼接
        })
        for (var i = 0; i < this.data.imgs2.length; i++) {
          wx.uploadFile({
            url: app.ReqUrl + 'user/addPhotos',
            filePath: that.data.imgs2[i],
            name: 'file',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': 'PHPSESSID=' + app.Cookie + ';path=/'
            },
            formData: {
              'openid_key': app.openid_key
            },
            success: res => {
              console.log(res);
            }
          })
        }
      }
    })
  },
  // 删除图片
  deleteImage: function(e) {
    console.log(e)
    var that = this;
    wx.showModal({
      title: '删除图片',
      content: '确认要删除吗？',
      showCancel: true,
      success: function(res) {
        if (res.cancel == true) {
          wx.showToast({
            title: '已取消',
          })
        } else if (res.confirm == true) {
          var imgId = e.currentTarget.dataset.index;
          var imgs = that.data.imgs2;
          console.log(imgs)
          imgs.splice(imgId, 1);
          console.log(imgs)
          that.setData({
            imgs2: imgs
          })
          wx.showToast({
            title: '删除成功',
          })
        }
      }
    })
  },
  // 获取地图地址
  map: function(e) {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          'inputValue[3].value': res.address
        })
      },
    })
  },
  // 传输表单数据接口
  formSubmit: function(e) {
    var that = this;
    var formData = e.detail.value;
    var warn = "";
    var flag = true;
    // form校验
    if (formData.tel == "") {
      warn = "请填写你的手机号码";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(formData.tel))) {
      warn = "手机号码格式不正确";
    } else if (formData.weixin == "") {
      warn = "请填写你的微信";
    } else if (formData.email == "") {
      warn = "请填写你的邮箱";
    } else if (formData.address == "") {
      warn = "请点击右方按钮选择你的地址";
    } else {
      flag = false;
      wx.redirectTo({
        url: '../index/index?pos=' + formData.pos + '&company' + formData.company + '&tel=' + formData.tel + '&weixin=' + formData.weixin + '&email=' + formData.email + '&address=' + formData.address + '&textarea=' + formData.textarea + '&http=' + formData.http + '&phone=' + formData.phone
      }) //返回首页并把数据带过去
    }
    // 如果信息填写不完整
    if (flag == true) {
      wx.showModal({
        title: '提示',
        content: warn
      })
    }
    // 本地缓存form数据
    wx.setStorage({
      key: 'form',
      data: formData,
    })

    // 接口4：传输数据到服务器
    app.postData('user/add', {
      data: {
        avatarUrl: this.data.userInfo.avatarUrl,
        name: this.data.userInfo.nickName,
        tel: formData.tel,
        company: formData.company,
        position: formData.pos,
        wechat: formData.weixin,
        email: formData.email,
        pro: this.data.userInfo.province,
        city: this.data.userInfo.city,
        area: 0,
        addr: formData.address,
        product: formData.textarea,
        http: formData.http,
        phone: formData.phone,
        img_url: this.data.imgs2,
        top_img: this.data.imgs,
      }
    }, res => {
      console.log('成功传回数据');
      console.log(JSON.stringify(res.data))
    })
    
  },
  collect: function() {
    wx.navigateTo({
      url: '../come/come',
    })
  },
  onLoad: function() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
})